package com.vf.cloud.paas.app.master.api.scene.controller;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.api.scene.service.ISceneClassify;
import com.vf.cloud.paas.app.master.common.domain.SceneClassify;
import com.vf.cloud.paas.app.master.common.domain.User;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import com.vf.cloud.paas.common.util.WebUtils;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/scene/classify")
public class SceneClassifyController {

	@Autowired
	private ISceneClassify sceneClassifyImpl;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<SceneClassify>> list(@RequestBody SceneClassify param) {
		List<SceneClassify> rootList = new LinkedList<SceneClassify>();
		SceneClassify root = new SceneClassify();
		root.setId("-1");
		root.setPid("root");
		root.setName("默认");
		root.setSort(0);
		root.setChildren(sceneClassifyImpl.getChildren(param,null));
		rootList.add(root);
		return R.ok(rootList);
	}

	@RequestMapping(value = "/lazyTree", method = RequestMethod.POST)
	public R<List<SceneClassify>> tree(@RequestBody SceneClassify param) {
		return R.ok(sceneClassifyImpl.getChildrens(param));
	}

	@RequestMapping(value = "/selectTree", method = RequestMethod.POST)
	public R<List<SceneClassify>> selectTree(HttpServletRequest request,@RequestBody SceneClassify param) {
		
		User user = WebUtils.getCurUser(request);
		if (user == null) {
			return R.expired();
		}
		
		List<SceneClassify> rootList = new LinkedList<SceneClassify>();
		SceneClassify root = new SceneClassify();
		root.setId("-1");
		root.setPid("root");
		root.setName("默认");
		root.setSort(0);
		root.setChildren(sceneClassifyImpl.getChildren(param,user));
		rootList.add(root);
		return R.ok(rootList);
	}

	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<SceneClassify> save(HttpServletRequest request, @RequestBody SceneClassify projectClassify) {
		User user = WebUtils.getCurUser(request);
		if (projectClassify == null) {
			return R.failed("参数必填.");
		}
		if (StrKit.isBlank(projectClassify.getId())) {
			projectClassify.setId(UuidUtil.getUUID());
			projectClassify.setCreateUserId(user.getId());
			if (StrKit.isBlank(projectClassify.getPid()))
				projectClassify.setPid("-1");
			if (projectClassify.save())
				return R.ok();
		} else {
			projectClassify.setModifyUserId(user.getId());
			if (projectClassify.update())
				return R.ok();
		}
		return R.failed();
	}

	@Tx
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody SceneClassify assetClassify) {
		try {
			if (assetClassify == null || StrKit.isBlank(assetClassify.getId())) {
				return R.failed("参数必填.");
			}
			sceneClassifyImpl.delete(assetClassify.getId());
			return R.ok();
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

}
